package com.i2bt.adapter;

import com.i2bt.R;
import com.i2bt.app.files.FilesUploadActivity;
import com.i2bt.bean.FileInfoBean;

import android.graphics.drawable.Drawable;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class FilesUploadListAdapter extends BaseAdapter {
	
	private FilesUploadActivity activity; // 文件移动活动类
	
	private List<FileInfoBean> filesUploadData; // 文件夹信息
	
	private SparseBooleanArray booleanArray;
	
	public FilesUploadListAdapter(FilesUploadActivity activity, List<FileInfoBean> filesUploadData) {
		this.activity = activity;
		if (filesUploadData == null) {
			filesUploadData = new ArrayList<FileInfoBean>();
		}
		this.filesUploadData = filesUploadData;
		booleanArray = new SparseBooleanArray();
	}

	@Override
	public int getCount() {
		return filesUploadData.size();
	}

	@Override
	public Object getItem(int position) {
		return filesUploadData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		// 初始化
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(activity).inflate(
					R.layout.item_files_upload_list, null, false);
			
			holder = new ViewHolder();
			holder.filesUploadImg = (ImageView) convertView.findViewById(
					R.id.files_upload_item_img);
			holder.filesUploadName = (TextView) convertView.findViewById(
					R.id.files_upload_item_name);
			holder.filesUploadCheckBox = (CheckBox) convertView.findViewById(
					R.id.files_upload_item_checkbox);
			
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		
		// 显示文件夹名
		FileInfoBean fileInfoBean = filesUploadData.get(position);
		holder.filesUploadName.setText(fileInfoBean.getFileName());
		
		holder.filesUploadImg.setImageDrawable(
				getFileIcon(fileInfoBean.getFileName(), fileInfoBean.isFile() ));
		
		// 跟随选择按钮的响应而改变文件列表的布尔数组
		holder.filesUploadCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				booleanArray.put(position, isChecked);
			}
		});
		
		// 设置按钮的选择状态
		boolean isChecked = booleanArray.get(position);
		holder.filesUploadCheckBox.setChecked(isChecked);
		
		// 使文件无法点击
		if (fileInfoBean.isFile()) {
			convertView.setFocusable(true);
		}
		else {
			convertView.setFocusable(false);
		}
		
		return convertView;
	}
	
	private class ViewHolder {
		
		ImageView filesUploadImg;
		
		TextView filesUploadName;
		
		CheckBox filesUploadCheckBox;
	}
	
	/**
	 * 
	 * @param fileName
	 * @param isFile
	 * @return
	 */
	private Drawable getFileIcon(String fileName, boolean isFile) {
		int imgId = 0;
		
		if (isFile) {
			
			String suffix = "";
			if (fileName.contains(".")) {
				suffix = fileName.substring(fileName.lastIndexOf("."))
						.toLowerCase(Locale.CHINA);
			}	
			
			if (suffix.equals(".mp3") || suffix.equals(".acc") 
					|| suffix.equals("APE") || suffix.equals("wav")) {
				imgId = R.drawable.files_icon_music;
			}
			else if (suffix.equals(".png") || suffix.equals(".jpg")
					|| suffix.equals(".bmp")) {
				imgId = R.drawable.files_icon_image;
			}
			else if (suffix.equals(".avi") || suffix.equals(".rmvb")
					|| suffix.equals(".mpg") || suffix.equals(".3gp")
					|| suffix.equals(".mp4") || suffix.equals(".rm")
					|| suffix.equals(".mkv") || suffix.equals(".flv")) {
				imgId = R.drawable.files_icon_movie;
			}
			else if (suffix.equals(".txt")){
				imgId = R.drawable.files_icon_txt;
			}
			else {
				imgId = R.drawable.files_icon_other;
			}
			
		}
		else {
			if (fileName.contains(":")) {
				imgId = R.drawable.files_icon_disk;
			}
			else {
				imgId = R.drawable.files_icon_folder;
			}
		}
		
		
		return activity.getResources().getDrawable(imgId);
	}
	
	/**
	 * 文件夹数据
	 * @param filesUploadData
	 */
	public void setData(List<FileInfoBean> filesUploadData) {
		this.filesUploadData = filesUploadData;
		notifyDataSetInvalidated();
	}
	
	public List<Integer> getCheckedList() {
		List<Integer> filesCheckedList = new ArrayList<Integer>();
		for (int i = 0; i < filesUploadData.size(); i++) {
			if (booleanArray.get(i)) {
				filesCheckedList.add(i);
			}
		}
		return filesCheckedList;
	}
	
}
